package com.kaibes.core.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class RegexButNullValidator implements ConstraintValidator<RegexButNull, String> {
    
    private String regex;

    @Override
    public void initialize(RegexButNull constraintAnnotation) {
        regex = constraintAnnotation.regex();
    }
    
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value != null) {
            return value.matches(regex);
        } else {
            return true;
        }
    }

}
